import requests
import json



'''
ak = 'ARAhQ54l9eTGmUhszx8iPZAF'
sk = 'aG5urwxj3U1FWidG1BIFWp79oyNVEky7'
host = 'https://aip.baidubce.com/oauth/2.0/\
token?grant_type=client_credentials&client_id=\
{}&client_secret={}'.format(ak,sk)
print(host)

header = {'Content-Type': 'application/json; charset=UTF-8'}
response = requests.get(host,headers = header)
access_token = response.json()['access_token']
print(access_token)
detect_url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?\
access_token={}'.format(access_token)

image_base64 = base64_image(
    r'C:/Users/Administrator/Desktop/1.jpg')

info_dict = {"image":image_base64,
             "image_type":"BASE64",
             "face_field":"faceshape,facetype,beauty,age,glasses"}
params = json.dumps(info_dict)

response_detect = requests.post(detect_url,
                                headers = header,
                                data = params)
return_info = response_detect.json()
print(return_info)
'''

class AccessToken:
    host_cla = 'https://aip.baidubce.com/oauth/2.0/\
token?grant_type=client_credentials&client_id=\
{}&client_secret={}'#获取access_token值的链接

    header = {'Content-Type': 'application/json; charset=UTF-8'}

    def __init__(self,
                 ak = 'ARAhQ54l9eTGmUhszx8iPZAF',
                 sk = 'aG5urwxj3U1FWidG1BIFWp79oyNVEky7',
                 img_64 = None,
                 img_path = None,
                 img_url = None,
                 face_token = None,
                 ):
        
        self.ak = ak
        
        self.sk = sk
        

        self.access_token = requests.get(
            self.host_cla.format(ak,sk),
            headers = self.header
            ).json()['access_token']

        if img_path != None:
            image_64 = self.base64_image(img_path)
            self.image,self.image_type = image_64,"BASE64"
            
        elif img_url != None:
            self.image,self.image_type = img_url,"URL"
            
        elif face_token != None:
            self.image,self.image_type = face_token,"FACE_TOKEN"
        elif img_64 != None:
            self.image,self.image_type = img_64,"BASE64"
            
            

    def base64_image(self,img_path):
        '''\
        img_path :图片连接
        return :  返回图片的base64编译字符串
        '''
        import base64
        with open(img_path,'rb') as f:
            base64_data = base64.b64encode(f.read())

        return str(base64_data,'utf-8')


    
class AddFace(AccessToken):

    
    def add_face_to_ku(self,
                       user_id,
                       user_info,
                       group_id = "group_repeat",
                       quality_control = "LOW",
                       liveness_control = "NORMAL",

                       ):

        url = 'https://aip.baidubce.com/rest/2.0/face/v3/\
faceset/user/add?access_token={}'.format(self.access_token)
        

        params = {"image":self.image,
                  "image_type":self.image_type,
                  "group_id":group_id,
                  "user_id":user_id,
                  "user_info":user_info,
                  "quality_control":quality_control,
                  "liveness_control":liveness_control}
        return_info = requests.post(url,
                                    headers = self.header,
                                    data = json.dumps(params),
                                    ).json()
        return return_info

        
class SearchFace(AccessToken):

    def search(self,group_id_list = "group_repeat"):
        
        url = 'https://aip.baidubce.com/rest/2.0/face/v3/search?\
access_token={}'.format(self.access_token)
        
        params = {"image":self.image,
                  "image_type":self.image_type,
                  "group_id_list":group_id_list,
                  "quality_control":"LOW",
                  "liveness_control":"NONE"}

        return_info = requests.post(url,
                                    headers = self.header,
                                    data = json.dumps(params),
                                    ).json()
        return return_info
        
    
if __name__ == "__main__":
    
    image_path = 'C:/Users/Administrator/Desktop/1802C班6月份一寸照/赵鑫荣14272619960405361X.jpg'
    x = SearchFace(img_path = image_path)
    info = x.search()
    print(info)

    


















